< previous page page_278 next page >

Page 278
EXERCISE
Add Code to the Close Command Button
1. Double-click the Close command button and add the following code:
Private Sub cmdClose_Click()
If cmdClose.Caption = "&Close" Then
Unload Me
Else

datEmployees.Recordset.CancelUpdate
If datEmployees.Recordset.EditMode <> adEditAdd Then
datEmployees.Recordset.Move 0
End If
Call ToggleButtons
End If
End Sub
analysis.gifIn this event procedure, you first need to check to see whether the Caption property of the Close command button is equal to &Close. If it is, you know that the user has not entered an editing mode and can therefore unload the form. If its not equal to &Close, then the Caption must be equal to &Cancel, which means you need to cancel the updating of data from happening. You apply the CancelUpdate method to the Recordset object of the data control to cancel the AddNew or editing of data from happening. If the recordset is in edit mode, you need to refresh the current record on the form. To do this, check the EditMode property of the Recordset object to verify that youre not doing an add operation. If this is the case, simply use the Move method to move zero records from the current record. This forces the data control to refresh all the controls on the form with the values in the recordset. The last thing to do is to toggle the command buttons back to their original nonediting state by calling ToggleButtons one more time.

Validating Data
When the user enters data into the text boxes, you may need to make sure the data is valid before you commit the data to the database. You can use a couple different methods to validate the data. Lets look at how you do this with the ADO data control.

 
< previous page page_278 next page >

If you like this book, buy it!